<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2018/10/16
 * Time: 17:14
 */

namespace app\index\model;


use app\common\RedisCache;

class VoteItems extends BaseModel
{
    const TABLE = 'vote_items';

    const DISPLAY_YES = 1;//显示
    const DISPLAY_NO = 0;//隐藏
    const ENABLE_YES = 1;//可以投票
    const ENABLE_NO = 0;//限制投票

    /**
     * @param $voteId
     * @param $mid
     * 获取参加活动的作品
     */
    public static function getItems($voteId,$typeId = null, $order = null, $where = null)
    {
        $key = 'vote_items_' . $voteId;
        $expireTime = 30 * 24 * 3600;
        $fields = 'a.id,a.vote_type_id,a.mid,a.name,a.description,a.info,a.enable,a.head_img,a.img,a.video_url,a.info_pick,a.total,a.sort,b.type_no,b.name as type_name,a.remark';
        //不作条件查询首先读取redis缓存
        if ($where === null) {
            $items = RedisCache::getInstance()->cache($key);
            if ($items !== false) {
                $items = json_decode($items, 1);
            } else {
                $items = self::alias('a')
                    ->join(VoteType::TABLE . ' b', 'a.vote_type_id = b.id', 'left')
                    ->where('a.vote_id', $voteId)
                    ->where('a.display', self::DISPLAY_YES)
                    ->order('a.sort desc,a.id desc')
                    ->field($fields)
                    ->select();
                RedisCache::getInstance()->cache($key, json_encode($items,JSON_UNESCAPED_UNICODE), $expireTime);
            }

            if ($items && $order == 'top') {
                array_multisort(array_column($items, 'total'), SORT_DESC, SORT_NUMERIC, $items);
            }
        } else {
            if (!$where)
                $where = '1=1';

            //是否排序
            $order = $order == 'top' ? 'a.total desc,a.id desc' : 'a.sort desc,a.id desc';
            $items = self::alias('a')
                ->join(VoteType::TABLE . ' b', 'a.vote_type_id = b.id', 'left')
                ->where('a.vote_id', $voteId)
                ->where('a.display', self::DISPLAY_YES)
                ->where($where)
                ->order($order)
                ->field($fields)
                ->select();
        }
        //按分类取
        if($items && $typeId){
            $new = [];
            foreach ($items as $v) {
                if($v['vote_type_id'] == $typeId){
                    $new[] = $v;
                }
            }
            $items = $new;
        }
        return $items;
    }

    public static function findOne($itemId)
    {
        $field = "a.id,a.vote_id,a.vote_type_id,a.mid,a.enable,a.head_img,a.img,a.video_url,a.info_pick,a.name,a.description,a.info,a.total,b.head_img as type_head_img,a.remark";
        $item = self::alias('a')
            ->join(VoteType::TABLE.' b','b.id=a.vote_type_id','left')
            ->where('a.id',$itemId)
            ->where('a.display',self::DISPLAY_YES)
            ->field($field)
            ->find();
        return $item;
    }


    public static function getOneItem($voteId, $itemId)
    {
        $items = self::getItems($voteId);
        if ($items) {
            foreach ($items as $v) {
                if ($v['id'] == $itemId) {
                    return $v;
                }
            }
        }
        return [];
    }
}